package awConnectionService;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.jws.WebService;

import entities.Device;
import entities.DeviceType;
import entities.User;

@WebService
public class ConnectionServiceImp implements ConnectionService{
	private Connection con;
	private DBConnect database;// = new DBConnect(con);
	private UserDB userDB;// = new UserDB(con);
	
	public ConnectionServiceImp(){
		getConnection();
		database = new DBConnect(con);
		userDB = new UserDB(con);
	}

	private void getConnection(){
		final String driver = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://localhost:3306";
		String userName = "root";
		String password = "alfa2082";
		String dbName = "capstone";
		
		try{
			Class.forName(driver).newInstance();
			con = DriverManager.getConnection(url, userName, password);
			
			Statement statement = con.createStatement();
			ResultSet rs = statement.executeQuery("SHOW DATABASES LIKE '" + dbName + "'");
			
			if(!rs.next()){
				System.out.println("NO DB");
				Statement stat = con.createStatement();
				
				stat.executeUpdate("CREATE DATABASE " + dbName + ";");
				stat.execute("USE " + dbName + ";");
				stat.executeUpdate(
						"CREATE TABLE devices (uuid CHAR(30) PRIMARY KEY, name CHAR(100), ip CHAR(50), type CHAR(7));"); 
				stat.executeUpdate(
						"CREATE TABLE connections (hostuuid CHAR(30), connecteduuid CHAR(30), PRIMARY KEY (hostuuid, connecteduuid));");
//				stat.executeUpdate()
				stat.close();
			}
			
			statement.close();
		}catch(SQLException ex){ex.printStackTrace();
		}catch (InstantiationException e) {e.printStackTrace();
		}catch (IllegalAccessException e) {e.printStackTrace();
		}catch (ClassNotFoundException e) {e.printStackTrace();
		}
	}
	
	//Windows
	public void addDevice(Device d) {
		System.out.println("ADD DEVICE");
		database.insertDevice(d);
	}

	public void updateDevice(Device d) {
		System.out.println("UPDATE DEVICE");
		database.updateDevice(d);		
	}

	public void removeDevice(Device d) {
		System.out.println("REMOVE DEVICE");
		database.removeDevice(d.getUUID());
	}

	public void connectDevices(String hostUUID, String targetIp, String pairKey) {
		Device host = database.getDeviceByUUID(hostUUID);
		Device target = database.getDeviceByIp(targetIp);
		
		String targetPairKey = database.getDevicePair(target.getUUID());
		if(targetIp.equals(targetPairKey)){
			host.addDevice(target.getUUID());
			target.addDevice(hostUUID);
			updateDevice(target);
			updateDevice(host);		
		}else{
			System.out.println("Invalid pair key");
		}
	}
	public Device getDeviceByUUID(String UUID) {
		Device d = new Device();
		d.setDeviceName("NOTFOUND");
		if(UUID != null){
			Device temp = database.getDeviceByUUID(UUID);
			if(!temp.getDeviceName().equalsIgnoreCase("NOTFOUND")){
				d = temp;
			}
		}
		return d;
	}
	
	public Device getDeviceByIp(String ip){
		System.out.println("GET DEVICE BY IP");
		return database.getDeviceByIp(ip);
	}
	public ArrayList<Device> getDevicesByUser(String userid){
		return database.getDevicesByUser(userid);
	}
	
	//Android
	public void addDeviceAnd(String UUID, String name, String ip, String type, ArrayList<String> connections){
		System.out.println("ANDROID ADD");
		Device d = new Device();
		d.setUUID(UUID);
		d.setDeviceName(name);
		d.setIpAddress(ip);
		d.setDeviceType(DeviceType.valueOf(type));
		
		if(connections.size() >= 1){
			System.out.println("ONE");
			if(!connections.get(0).equals("")){
				System.out.println("help");
				d.setConnectedDevices(connections);
			}else{
				d.setConnectedDevices(null);
				System.out.println("ELSE");
			}
		}
		System.out.println(d.getConnectedDevices());
		
		
		addDevice(d);
	}
	
	public void removeDeviceAnd(String UUID, String name, String ip, String type, ArrayList<String> connections){
		System.out.println("ANDROID REMOVE");
		Device d = new Device();
		d.setUUID(UUID);
		d.setDeviceName(name);
		d.setIpAddress(ip);
		d.setDeviceType(DeviceType.valueOf(type));
		d.setConnectedDevices(connections);
		
		removeDevice(d);
	}
	
	public void updateDeviceAnd(String UUID, String name, String ip, String type, ArrayList<String> connections){
		System.out.println("ADNROID UPDATE");
		Device d = new Device();
		d.setUUID(UUID);
		d.setDeviceName(name);
		d.setIpAddress(ip);
		d.setDeviceType(DeviceType.valueOf(type));
		d.setConnectedDevices(connections);
		
		updateDevice(d);
	}
	
	//User
	public void addUserAnd(String username, String password, String fname, String lname, String email, String role){
		System.out.println("ConnectionServiceImp: addUserAnd()");
		
		User user = new User();
		
		user.setUsername(username);
		user.setPassword(password);
		user.setFname(fname);
		user.setLname(lname);
		user.setEmail(email);
		user.setRole(role);
		
		userDB.addUser(user);
	}
	public void addUser(User user){
		System.out.println("ConnectionServiceImp: addUser()");
		
		userDB.addUser(user);
	}
	public User getUser(String username){
		System.out.println("ConnectionServiceImp: getUser()");
		
		User user = userDB.getUser(username);
		return user;
	}
	public void addUserDevice(String uuid, String id){
		System.out.println("ConnectionServiceImp: addUserDevice()");
		
		userDB.addUserdevice(uuid, Integer.parseInt(id));
	}
	
	public void insertDevicePair(String uuid, String pairKey){
		database.insertDevicePair(uuid, pairKey);
	}
	public void removeDevicePair(String uuid){
		database.removeDevicePair(uuid);
	}
	public void updateDevicePair(String uuid, String pairKey){
		database.updateDevicePair(uuid, pairKey);
	}
	public String getDevicePair(String uuid){
		String pairKey = database.getDevicePair(uuid);
		System.out.println(pairKey + "PairKey");
		return pairKey;
	}
	
	
}
